package cn.devzyh.oms.coms.order.service;

import cn.devzyh.oms.coms.api.order.dto.OrderDTO;
import cn.devzyh.oms.coms.api.order.entity.OrderDetail;
import cn.devzyh.oms.coms.api.order.entity.OrderHeader;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 销售订单单头
 *
 * @author devzyh
 * @date 2024-03-06
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderService {

	private final OrderHeaderService headerService;

	private final OrderDetailService detailService;

	/**
	 * 获取订单
	 *
	 * @param id
	 * @return
	 */
	OrderDTO getOrder(Long id) {
		OrderHeader header = headerService.getById(id);
		if (header == null) {
			return null;
		}

		LambdaQueryWrapper<OrderDetail> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(OrderDetail::getOrderId, id);
		List<OrderDetail> details = detailService.list(wrapper);
		if (CollectionUtil.isEmpty(details)) {
			return null;
		}

		OrderDTO order = new OrderDTO();
		order.setHeader(header);
		order.setDetails(details);
		return order;
	}

}